import json
import time
from datetime import datetime
# 读取文件数据用于调试
from read_filedata import read_shipdata,read_shipdata2,read_planedata,read_areadata2,write_actiondata
# 信号开关行为识别算法
from signal_check.signal_plane import check_signal_plane
from signal_check.signal_ship import check_signal_ship
# 船舶与重要区域行为识别算法
from ship_area_check.areacheck_ship import check_impArea_port
# 导入飞机行为检测模块
from plane_hebavior_check.behavior_plane import process_plane_behavior, save_plane_behavior_results
# 轨迹补全算法
# from imputation.imputation import impute

if __name__ == '__main__':
    # 是否处理飞机数据
    process_plane = False
    # 是否处理船舶数据
    process_ship = True
    # 是否识别信号开关
    process_ship_signal = True
    # 是否识别区域行为
    process_ship_area = True
    # 是否处理轨迹补全
    process_impute = False
    
    # 处理飞机数据
    if process_plane:
        # # 读取文件生成飞机实时状态类输入数据
        # data_input, now_timestamp = read_planedata()
        # # print(f'data_input: {data_input}\n')
        # # 运行算法程序-adsb信号开关
        # action_signal_plane = check_signal_plane(data_input, now_timestamp)
        # print(f'\ncheck_signal_plane: {action_signal_plane}')
        print("\n===== 开始处理飞机行为识别 =====")
        # 读取文件生成飞机实时状态类输入数据
        plane_data, plane_timestamp = read_planedata()
        print(f'读取飞机数据成功: {len(plane_data)} 条轨迹')
        
        # 使用process_plane_behavior函数执行所有行为检测
        results = process_plane_behavior(plane_data, plane_timestamp)
        
        # 打印结果概要
        for behavior_type, behavior_data in results.items():
            print(f'{behavior_type}: 检测到 {len(behavior_data)} 条记录')
        
        # 保存结果
        save_plane_behavior_results(results)
        print(f'已将飞机行为记录写入 ./data/track_action.csv')
        
        print("===== 飞机行为识别处理完成 =====\n")
    



    
    # 处理船舶数据
    if process_ship:
        # 读取文件生成时序轨迹类输入数据
        # ship_data, now_timestamp = read_shipdata()
        # print(f'read_shipdata: {ship_data},{now_timestamp}\n')
        # # 运行算法程序
        # action_signal_ship = check_signal_ship(ship_data, now_timestamp)
        # # print(f'\ncheck_signal_ship: {action_signal_ship}')
        # # 行为写入文件
        # write_actiondata(action_signal_ship)
        # # 运行轨迹补全算法
        # imputed_data = impute(ship_data, )
        # print(f'imputed_data: {imputed_data}')

        # 读取重要区域数据
        area_list = read_areadata2()
        # 分时间段调用
        # 读取文件生成时序轨迹类输入数据
        timestr_list = [
            # '2025-01-01 00:00:00',
            '2025-02-01 00:00:00',
            '2025-03-01 00:00:00',
            '2025-04-01 00:00:00',
            '2025-05-01 00:00:00',
            '2025-06-01 00:00:00',
            '2025-07-01 00:00:00'
        ]
        prev_timestamp = datetime.strptime('2025-01-01 00:00:00', '%Y-%m-%d %H:%M:%S').timestamp()
        for timestr in timestr_list:
            timestamp = datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S').timestamp()
            # 读取文件数据并执行算法
            data_input, now_timestamp = read_shipdata2(prev_timestamp, timestamp)
            if timestr == timestr_list[0]:
                continue

            if process_ship_signal:
                # 检测信号关闭与开启
                action_signal_ship = check_signal_ship(data_input, now_timestamp)
                # print(f'\ncheck_signal_ship: {action_signal_ship}')
                write_actiondata(action_signal_ship, True) # 行为写入文件
            
            if process_ship_area:
                # 检测是否出入重点区域或者港口
                action_check_area = check_impArea_port(data_input, area_list)
                print(f'\check_impArea_port: {action_check_area}')
                write_actiondata(action_check_area, True) # 行为写入文件
            
            prev_timestamp = timestamp
            time.sleep(1)
